//
//  JTDriverTrip.m
//  JiongTu
//
//  Created by 杨璟 on 16/4/1.
//  Copyright © 2016年 YSKJ. All rights reserved.
//

#import <AVOSCloud.h>

#import "JTDriverTrip.h"
#import "JTPassengerTrip.h"

#import "JTConst.h"

#import "JTUser.h"

#import <MJExtension.h>

@implementation JTDriverTrip

MJExtensionCodingImplementation

- (NSString *)description {
    NSString *startLocation =  self.startLocation;
    NSString *startStreet =  self.startStreet;
    NSString *start =  self.start;
    NSString *endLocation =  self.endLocation;
    NSString *endStreet =  self.endStreet;
    NSString *end =  self.end;
    NSString *time =  self.time;
    NSString *timeInterval =  self.timeInterval;
    NSString *seatCount =  [self.seatCount stringValue];
    NSString *identity =  self.identity;
    NSString *remark =  self.remark;
    NSString *currentSeatCount =  [self.currentSeatCount stringValue];
    NSString *price =  [self.price stringValue];
    
    NSString *objectId =  self.objectId;
    NSString *createAt =  [self.createAt description];
    NSString *updateAt =  [self.updateAt description];
    
    NSString *driver =  [self.driver description];
    NSString *passenger =  [self.passenger description];
    
    NSString *str = [NSString stringWithFormat:@"JTDriverTrip:[\n objectId:%@\n createAt:%@\n updateAt:%@\n startLocation:%@\n startStreet:%@\n start:%@\n endLocation:%@\n endStreet:%@\n end:%@\n time:%@\n timeInterval:%@\n seatCount:%@\n identity:%@\n remark:%@\n currentSeatCount:%@\n price:%@\n driver:%@\n passenger:%@\n]", objectId, createAt, updateAt, startLocation, startStreet, start, endLocation, endStreet, end, time, timeInterval, seatCount, identity, remark, currentSeatCount, price, driver, passenger];
    
    return str;
}

+ (NSMutableArray *)tripWithAVObjects:(NSArray *)objects {
    NSMutableArray *mutableArray = [NSMutableArray array];
    
    for (AVObject *obj in objects) {
        JTDriverTrip *trip = [[JTDriverTrip alloc] init];
        trip.startLocation = [obj objectForKey:kJTJourneyKeyStart];
        trip.startStreet = [obj objectForKey:kJTJourneyKeyStartStreet];
        trip.start = [obj objectForKey:@"Start"];
        trip.endLocation = [obj objectForKey:kJTJourneyKeyEnd];
        trip.endStreet = [obj objectForKey:kJTJourneyKeyEndStreet];
        trip.end = [obj objectForKey:@"End"];
        trip.time = [obj objectForKey:kJTJourneyKeyDate];
        trip.timeInterval = [obj objectForKey:kJTJourneyKeyTimeInterval];
        trip.seatCount = [obj objectForKey:kJTJourneyKeySeats];
        trip.identity = [obj objectForKey:kJTJourneyKeyIdentity];
        trip.remark = [obj objectForKey:kJTJourneyKeyRemark];
        trip.currentSeatCount = [obj objectForKey:kJTJourneyKeyPcurrent];
        trip.price = [obj objectForKey:@"Price"];
        trip.currentSeatCount =[obj objectForKey:@"current"];
        trip.objectId = [obj objectForKey:@"objectId"];
        trip.createAt = [obj objectForKey:@"createdAt"];
        trip.updateAt = [obj objectForKey:@"updatedAt"];
        AVUser *driver = [obj objectForKey:@"DriverInformation"];
        trip.driver = [JTUser userWithAVUser:driver];
        trip.passenger = [obj objectForKey:@"Passenger"];
        
        [mutableArray addObject:trip];
    }
    
    return mutableArray;
}

+ (instancetype)tripWithAVObject:(AVObject *)object {
    JTDriverTrip *oneTrip = [[self tripWithAVObjects:@[object]] firstObject];
    
    return oneTrip;
}

//+ (instancetype)tripWithPointer:(AVObject *)pointer {
//    AVQuery * query = [AVQuery queryWithClassName:kJTClassJourneyDriver];
//    AVObject *object = [query getObjectWithId:pointer.objectId];
//    
//    return [self tripWithAVObject:object];
//}

- (void)setPassenger:(NSArray *)passenger {
    NSMutableArray *passengers = [NSMutableArray array];
    for (NSString *onePassenger in passenger) {
        AVQuery * query = [AVQuery queryWithClassName:kJTClassJourneyPassenger];
        AVObject *object = [query getObjectWithId:onePassenger];
        if (object) {
            JTPassengerTrip *oneTrip = [JTPassengerTrip tripWithAVObject:object];
            [passengers addObject:oneTrip];
        }
    }
    _passenger = passengers;
}

@end
